package com.flycms.web.front.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.soft.model.Case;
import com.flycms.module.soft.model.Soft;
import com.flycms.module.soft.model.SoftCategory;
import com.flycms.module.soft.model.SoftDetial;
import com.flycms.module.soft.model.SoftScene;
import com.flycms.module.soft.service.SoftCaseService;
import com.flycms.module.soft.service.SoftCategoryService;
import com.flycms.module.soft.service.SoftSceneService;
import com.flycms.module.soft.service.SoftService;
import com.flycms.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author huangzt
 * @version 1.0
 * @date 2020/3/7 19:57
 */
@RestController
@RequestMapping(value = "/rest/soft")
public class SoftController {
    @Autowired
    SoftService softService;
    @Autowired
    SoftCategoryService softCategoryService;
    @Autowired
    SoftCaseService softCaseService;
    @Autowired
    SoftSceneService softSceneService;

    /**
     * 获取软件列表
     *
     * @param softName
     * @param softPlatform
     * @param status
     * @param categoryId
     * @param pageIndex
     * @param pageSize
     * @return RestDataVo
     */
    @GetMapping(value = "/list")
    public RestDataVo softList(@RequestParam(value = "softName", required = false) String softName,
                               @RequestParam(value = "softPlatform", required = false) String softPlatform,
                               @RequestParam(value = "status", required = false) Integer status,
                               @RequestParam(value = "categoryId", required = false) Integer categoryId,
                               @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                               @RequestParam(value = "pageSize", required = false) Integer pageSize,
                               @RequestParam(value = "categoryName", required = false) String orderby,
                               @RequestParam(value = "categoryName", required = false) String order) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        pageSize = pageSize == null ? 10 : pageSize;
        if (status == null) {
            status = 1;
        }
        PageVo<Soft> pageVo = softService.queryByPage(softName, categoryId, softPlatform, status, orderby, order, pageIndex, pageSize);
        return RestDataVo.PAGE(pageVo.getList(), pageIndex, pageSize, pageVo.getCount());
    }

    /**
     * 获取软件分类列表
     *
     * @param categoryName
     * @param pageIndex
     * @param pageSize
     * @return RestDataVo
     */
    @GetMapping(value = "/category/list")
    public RestDataVo softCategoryList(@RequestParam(value = "categoryName", required = false) String categoryName,
                                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                       @RequestParam(value = "orderby", required = false) String orderby,
                                       @RequestParam(value = "order", required = false) String order) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        pageSize = pageSize == null ? 10 : pageSize;
        PageVo<SoftCategory> pageVo = softCategoryService.queryByPage(categoryName, orderby, order, pageIndex, pageSize);
        return RestDataVo.PAGE(pageVo.getList(), pageIndex, pageSize, pageVo.getCount());
    }


    /**
     * 获取软件详情
     *
     * @param softId
     * @param status
     * @return RestDataVo
     */
    @GetMapping(value = "/detail")
    public RestDataVo softDetail(@RequestParam(value = "softId", required = false) Integer softId,
                                 @RequestParam(value = "status", required = false) Integer status) {
        if (softId == null) {
            return RestDataVo.ERROR("参数错误");
        }
        if (status == null) {
            status = 1;
        }
        Soft softVo = softService.queryByPk(softId, status);
        SoftDetial softDetial = ConvertUtils.sourceToTarget(softVo, SoftDetial.class);
        SoftCategory softCategory = softCategoryService.queryByPk(softVo.getCategory_id());
        softDetial.setCategory_name(softCategory.getCategory_name());
        String[] caseId = softVo.getCase_ids().split(",");
        List<Integer> caseIds = new ArrayList<>(16);
        for (int i = 0; i <caseId.length ; i++) {
            caseIds.add(Integer.parseInt(caseId[i]));
        }
        List<Case> caseList = softCaseService.queryByIds(caseIds);
        softDetial.setCaseList(caseList);
        List<SoftScene> sceneList = softSceneService.queryByPage(null,softId,null,null,1,5).getList();
        softDetial.setSceneList(sceneList);
        return RestDataVo.OK("查询成功", softDetial);
    }

    /**
     * 获取分类详情
     *
     * @param categoryId
     * @return RestDataVo
     */
    @GetMapping(value = "/category/detail")
    public RestDataVo softCategoryDetail(@RequestParam(value = "id", required = false) Integer categoryId) {
        if (categoryId == null) {
            return RestDataVo.ERROR("参数错误");
        }
        SoftCategory category = softCategoryService.queryByPk(categoryId);
        return RestDataVo.OK("查询成功", category);
    }

    /**
     * 后台管理员新增分类
     *
     * @param softCategory
     * @return RestDataVo
     */
    @PostMapping(value = "/system/category/add")
    public RestDataVo categoryAdd(@RequestBody @Valid SoftCategory softCategory) {
        try {
            softCategory.setCreate_time(new Date());
            softCategory.setUpdate_time(new Date());
            softCategoryService.insert(softCategory);
            return RestDataVo.OK("新增成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("新增失败，异常：" + e.getMessage());
        }
    }


    /**
     * 后台管理员发布软件
     *
     * @param soft
     * @return RestDataVo
     */
    @PostMapping(value = "/system/add")
    public RestDataVo softAdd(@RequestBody @Valid SoftDetial soft) {
        try {
            if(soft.getSoft_name().length()>=30){
                return RestDataVo.ERROR("软件名称不可超过30个字符");
            }
            if(soft.getSoft_platform().length()>=30){
                return RestDataVo.ERROR("软件平台不可超过30个字符");
            }
            if(soft.getSoft_version().length()>=30){
                return RestDataVo.ERROR("软件版本不可超过30个字符");
            }
            softService.insertSoftDetail(soft);
            return RestDataVo.OK("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestDataVo.ERROR("新增失败，异常：" + e.getMessage());
        }
    }


    /**
     * 后台管理员更新软件
     *
     * @param soft
     * @return RestDataVo
     */
    @PostMapping(value = "/system/edit")
    public RestDataVo softEdit(@RequestBody @Valid Soft soft) {
        try {
            if(soft.getSoft_name().length()>=30){
                return RestDataVo.ERROR("软件名称不可超过30个字符");
            }
            if(soft.getSoft_platform().length()>=30){
                return RestDataVo.ERROR("软件平台不可超过30个字符");
            }
            if(soft.getSoft_version().length()>=30){
                return RestDataVo.ERROR("软件版本不可超过30个字符");
            }
            soft.setUpdate_time(new Date());
            softService.update(soft);
            return RestDataVo.OK("修改软件产品成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("修改软件产品失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员场景列表
     * @param softId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/system/scene/list")
    public RestDataVo softSceneList(@RequestParam(value = "softId") Integer softId,
                                    @RequestParam(value = "pageIndex") Integer pageIndex,
                                    @RequestParam(value = "pageSize") Integer pageSize) {
        PageVo<SoftScene> softScenePageVo = softSceneService.queryByPage(null, softId, null, null, pageIndex, pageSize);
        return RestDataVo.PAGE(softScenePageVo.getList(), pageIndex, pageSize, softScenePageVo.getCount());
    }

    /**
     * 后台管理员新增软件应用场景
     *
     * @param softScene
     * @return RestDataVo
     */
    @PostMapping(value = "/system/scene/add")
    public RestDataVo softSceneAdd(@RequestBody @Valid SoftScene softScene) {
        try {
            //单个新增
            softScene.setCreate_time(new Date());
            softScene.setUpdate_time(new Date());
            softSceneService.insert(softScene);
            return RestDataVo.OK("新增应用场景成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("新增应用场景失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员更新软件应用场景
     *
     * @param softScene
     * @return RestDataVo
     */
    @PostMapping(value = "/system/scene/edit")
    public RestDataVo softSceneEdit(@RequestBody @Valid SoftScene softScene) {
        try {
            //单个修改
            SoftScene pk = softSceneService.queryByPk(softScene.getId());
            softScene.setCreate_time(pk.getCreate_time());
            softScene.setUpdate_time(new Date());
            softSceneService.update(softScene);
            return RestDataVo.OK("修改应用场景成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("修改应用场景失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员删除软件应用场景
     *
     * @param id
     * @return RestDataVo
     */
    @PostMapping(value = "/system/scene/delete")
    public RestDataVo softSceneDelete(@RequestParam(value = "id") String id) {
        if(!NumberUtils.isNumber(id)){
            return RestDataVo.ERROR("删除应用场景失败，参数错误");
        }
        try {
            SoftScene softScene = softSceneService.queryByPk(Long.parseLong(id));
            if(softScene==null){
                return RestDataVo.ERROR("删除应用场景失败,不存在或已被删除");
            }
            softSceneService.delete(Long.parseLong(id));
            return RestDataVo.OK("删除应用场景成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("删除应用场景失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员更新软件所属分类
     *
     * @param id
     * @param categoryId
     * @return RestDataVo
     */
    @PostMapping(value = "/system/edit/category")
    public RestDataVo softEditCategory(@RequestParam(value = "id") Integer id,
                                       @RequestParam(value = "categoryId") Integer categoryId) {
        try {
            Soft soft = softService.queryByPk(id, 1);
            if (soft == null) {
                return RestDataVo.ERROR("修改失败，该软件不存在或已被删除");
            }
            soft.setUpdate_time(new Date());
            soft.setCategory_id(categoryId);
            softService.update(soft);
            return RestDataVo.OK("修改成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("修改失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员更新软件分类
     *
     * @param softCategory
     * @return RestDataVo
     */
    @PostMapping(value = "/system/category/edit")
    public RestDataVo categoryEdit(@RequestBody @Valid SoftCategory softCategory) {
        try {
            SoftCategory category = softCategoryService.queryByPk(softCategory.getId());
            if (category==null){
                return RestDataVo.ERROR("修改分类失败,不存在或已被删除");
            }
            softCategory.setUpdate_time(new Date());
            softCategory.setCreate_time(category.getCreate_time());
            softCategoryService.update(softCategory);
            return RestDataVo.OK("修改分类成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("修改分类失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员删除软件
     *
     * @param id
     * @return RestDataVo
     */
    @PostMapping(value = "/system/delete")
    public RestDataVo softDelete(@RequestParam(value = "id") Integer id) {
        try {
            softService.delete(id);
            return RestDataVo.OK("删除成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("删除失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员删除软件分类
     *
     * @param id
     * @return RestDataVo
     */
    @PostMapping(value = "/system/category/delete")
    public RestDataVo categoryDelete(@RequestParam(value = "id") Integer id) {
        try {
            PageVo<Soft> softPageVo = softService.queryByPage(null, id, null, null, null, null, 1, 2);
            if (softPageVo.getCount() != 0) {
                return RestDataVo.ERROR("删除失败,该分类下存有软件");
            }
            PageVo<Case> casePageVo = softCaseService.queryByPage(null,id,null,null,1,2);
            if(casePageVo.getCount()!=0){
                return RestDataVo.ERROR("删除失败,该分类下存有客户案例");
            }
            softCategoryService.delete(id);
            return RestDataVo.OK("删除成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("删除失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员新增客户案例
     */
    @PostMapping(value = "/system/case/add")
    public RestDataVo caseAdd(@RequestBody @Valid Case caseVo) {
        try {
            caseVo.setCreate_time(new Date());
            caseVo.setUpdate_time(new Date());
            softCaseService.insert(caseVo);
            return RestDataVo.OK("新增客户案例成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("新增失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员修改客户案例
     */
    @PostMapping(value = "/system/case/edit")
    public RestDataVo caseEdit(@RequestBody @Valid Case caseVo) {
        try {
            Case aCase = softCaseService.queryByPk(caseVo.getId());
            if (aCase==null){
                return RestDataVo.ERROR("修改客户案例失败，不存在或已被删除");
            }
            caseVo.setCreate_time(aCase.getCreate_time());
            caseVo.setUpdate_time(new Date());
            softCaseService.update(caseVo);
            return RestDataVo.OK("修改客户案例成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("修改失败，异常：" + e.getMessage());
        }
    }

    /**
     * 后台管理员删除客户案例
     */
    @PostMapping(value = "/system/case/delete")
    public RestDataVo caseDelete(@RequestParam(value = "id") Integer id) {
        try {
            Case aCase = softCaseService.queryByPk(id);
            if (aCase==null){
                return RestDataVo.ERROR("修改失败，不存在或已被删除");
            }
            softCaseService.delete(id);
            return RestDataVo.OK("删除客户案例成功");
        } catch (Exception e) {
            return RestDataVo.ERROR("删除失败，异常：" + e.getMessage());
        }
    }

    /**
     * 获取客户案例列表
     *
     * @param caseName
     * @param pageIndex
     * @param pageSize
     * @return RestDataVo
     */
    @GetMapping(value = "/case/list")
    public RestDataVo softCaseList(@RequestParam(value = "caseName", required = false) String caseName,
                                   @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                   @RequestParam(value = "orderby", required = false) String orderby,
                                   @RequestParam(value = "order", required = false) String order) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        pageSize = pageSize == null ? 10 : pageSize;
        PageVo<Case> pageVo = softCaseService.queryByPage(caseName, categoryId, orderby, order, pageIndex, pageSize);
        return RestDataVo.PAGE(pageVo.getList(), pageIndex, pageSize, pageVo.getCount());
    }

    /**
     * 获取客户案例列表
     *
     * @param id
     * @return RestDataVo
     */
    @GetMapping(value = "/case/detail")
    public RestDataVo softCaseDetail(@RequestParam(value = "id", required = false) String id) {
        if(!NumberUtils.isNumber(id)){
            return RestDataVo.ERROR("参数格式错误");
        }
        Case aCase = softCaseService.queryByPk(Integer.parseInt(id));
        return RestDataVo.OK(aCase);
    }


}
